<template>
  <div>
    <div class="action-bar">
      <!-- <el-upload
        accept=".xlsx, .xls"
        :headers="headers"
        :action="action"
        :on-success="handleSuccess"
        :on-error="handleError"
      >
        <el-button size="mini" icon="el-icon-upload2">导入</el-button>
      </el-upload>-->
      <!-- <el-button size="mini" icon="el-icon-download" @click="exportExcel($parent.selections)">导出</el-button> -->
      <el-button v-role="['worker']" type="primary" size="mini" icon="el-icon-plus" @click="add">新建</el-button>
    </div>
    <FormDialog title="新建" class="fixed-dialog-height" :show="dialogFormVisible" width="80%" :footer-show="false" @on-cancel="cancel">
      <EditForm v-if="dialogFormVisible" ref="editForm" />
    </FormDialog>
  </div>
</template>
<script>
import FormDialog from '@neu/biz-core/components/FormDialog'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import EditForm from './editForm'
import { getToken } from '@neu/biz-core/utils/auth'
import { download } from '@neu/dcom-ui/api/monthWeekly'
import { isArray, formatDate } from '@neu/biz-core/utils/utils'
export default {
  name: 'MonthWeeklyActionBar',
  components: {
    FormDialog,
    EditForm
  },
  data() {
    return {
      dialogFormVisible: false,
      action: '',
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Authentication': getToken()
      },
      fileName: '月报' + formatDate(new Date())
    }
  },
  methods: {
    // 新建用户按钮点击处理
    add() {
      this.dialogFormVisible = true
    },
    // 取消
    cancel() {
      this.dialogFormVisible = false
      this.$parent.refresh()
    },
    // 导出
    exportExcel(selections) {
      if (isArray(selections)) {
        if (selections.length === 0) {
          this.$message({
            type: 'warning',
            message: '请选择一条数据！'
          })
          return
        }
        if (selections.length > 1) {
          this.$message({
            type: 'warning',
            message: '只能选择一条数据！'
          })
          return
        }

        download(selections[0].id).then(res => {
          const content = res
          const blob = new Blob([content])
          const fileName = this.fileName + '.docx'
          // not IE downLoad
          if ('download' in document.createElement('a')) {
            const elink = document.createElement('a')
            elink.download = fileName
            elink.style.display = 'none'
            elink.href = URL.createObjectURL(blob)
            document.body.appendChild(elink)
            elink.click()
            URL.revokeObjectURL(elink.href)
            document.body.removeChild(elink)
          } else {
            navigator.msSaveBlob(blob, fileName)
          }
        })
      }
    },
    // 上传文件成功
    handleSuccess(response, file, fileList) {
      this.$message({
        showClose: true,
        message: '文件上传成功',
        type: 'success'
      })
    },
    // 上传文件失败
    handleError() {
      this.$message({
        showClose: true,
        message: '文件上传失败',
        type: 'error'
      })
    }
  }
}
</script>
